<template>
  <div>
    <el-form label-width="120px">
      <el-form-item label="为博客添加页码">
        <el-input type="text" v-model="page" width="200"></el-input>
      </el-form-item>
      <el-form-item label="为博客添加标题">
        <el-input type="text" v-model="title" width="200"></el-input>
      </el-form-item>
      <el-form-item label="为草稿添加标题">
        <el-input type="text" v-model="draftTitle" width="200"></el-input>
      </el-form-item>
    </el-form>
    <!-- 添加标签 -->
    <div class="setTag">
      <p>为博客添加标签:</p>
      <div @click="showTagsValue">
        <el-tag type="success">javascript</el-tag>
        <el-tag type="info">vue</el-tag>
        <el-tag type="warning">日记</el-tag>
        <el-tag type="danger">koa</el-tag>
        <el-tag type="danger">小项目</el-tag>
      </div>
    </div>
    <div class="editor">
      <div ref="toolbar" class="toolbar"></div>
      <div ref="editor" class="text"></div>
    </div>
    <el-button @click="send">发表博客</el-button>
    <el-button @click="save">保存到草稿箱</el-button>
  </div>
</template>

<script>
import E from "wangeditor";
import axios from "axios";
import draft from "./draft.vue";
export default {
  name: "editoritem",
  data() {
    return {
      // uploadPath,
      editor: null,
      info_: null,
      sort: "", //博客分类标签
      page: "", //页码
      title: "", //博客标题
      draftTitlefromchild: "", //子传父的数据
      bbb: "我是父组件的数据,也是邹智敏写的", //父传子
      draftTitle: "", //草稿标题
      newBlogTitle: []
    };
  },
  components: { draft },
  model: {
    prop: "value",
    event: "change"
  },
  //子组件用props:{}接收父组件的数据
  props: {
    value: {
      type: String,
      default: ""
    },
    isClear: {
      type: Boolean,
      default: false
    },
    paramValue: String,
    draft: { type: String },
    write: { type: String }
  },
  watch: {
    isClear(val) {
      // 触发清除文本域内容
      if (val) {
        this.editor.txt.clear();
        this.info_ = null;
      }
    },
    value: function(value) {
      if (value !== this.editor.txt.html()) {
      }
    }
    //value为编辑框输入的内容，这里我监听了一下值，当父组件调用得时候，如果给value赋值了，子组件将会显示父组件赋给的值
  },
  mounted() {
    console.log(this.draft);
    this.seteditor();
    this.editor.txt.html(this.draft);
  },
  methods: {
    seteditor() {
      // http://192.168.2.125:8080/admin/storage/create
      this.editor = new E(this.$refs.toolbar, this.$refs.editor);
      this.editor.customConfig.uploadImgShowBase64 = false; // base 64 存储图片
      this.editor.customConfig.uploadImgServer =
        "http://otp.cdinfotech.top/file/upload_images"; // 配置服务器端地址
      this.editor.customConfig.uploadImgHeaders = {}; // 自定义 header
      this.editor.customConfig.uploadFileName = "file"; // 后端接受上传文件的参数名
      this.editor.customConfig.uploadImgMaxSize = 2 * 1024 * 1024; // 将图片大小限制为 2M
      this.editor.customConfig.uploadImgMaxLength = 6; // 限制一次最多上传 3 张图片
      this.editor.customConfig.uploadImgTimeout = 3 * 60 * 1000; // 设置超时时间

      // 配置菜单
      this.editor.customConfig.menus = [
        "head", // 标题
        "video", // 插入视频
        "image", // 插入图片
        "undo", // 撤销
        "bold", // 粗体
        "fontSize", // 字号
        "fontName", // 字体
        "italic", // 斜体
        "underline", // 下划线
        "strikeThrough", // 删除线
        "foreColor", // 文字颜色
        "backColor", // 背景颜色
        "link", // 插入链接
        "list", // 列表
        "justify", // 对齐方式
        "quote", // 引用
        "emoticon", // 表情
        "table", // 表格
        "code", // 插入代码
        "redo" // 重复
        // "fullscreen" // 全屏
      ];

      this.editor.customConfig.uploadImgHooks = {
        fail: (xhr, editor, result) => {
          // 插入图片失败回调
        },
        success: (xhr, editor, result) => {
          // 图片上传成功回调
        },
        timeout: (xhr, editor) => {
          // 网络超时的回调
        },
        error: (xhr, editor) => {
          // 图片上传错误的回调
        },
        customInsert: (insertImg, result, editor) => {
          // 图片上传成功，插入图片的回调
          //result为上传图片成功的时候返回的数据，这里我打印了一下发现后台返回的是data：[{url:"路径的形式"},...]
          // console.log(result.data[0].url)
          //insertImg()为插入图片的函数
          //循环插入图片
          // for (let i = 0; i < 1; i++) {
          // console.log(result)
          let url = "http://otp.cdinfotech.top" + result.url;
          insertImg(url);
          // }
        }
      };
      this.editor.customConfig.onchange = html => {
        this.info_ = html; // 绑定当前逐渐地值
        this.$emit("change", this.info_); // 将内容同步到父组件中
      };
      // 创建富文本编辑器
      this.editor.create();
    },
    send() {
      localStorage.newBlog = parseInt(localStorage.newBlog) + 1;
      sessionStorage.newBlogTitle = sessionStorage.newBlogTitle + this.title;
      const data = {
        test: this.info_,
        sort: localStorage.sort,
        page: this.page,
        title: this.title
      };
      localStorage.info_ = JSON.stringify(this.info_);
      axios({
        url: "http://localhost:3000/publish",
        method: "post",
        data: data
      }).then(response => {});
    },
    getValue() {},
    showTagsValue(e) {
      console.log(e.target.innerHTML);
      localStorage.sort = e.target.innerHTML;
    },
    save() {
      const data = {
        draftContent: this.info_,
        draftTitle: this.draftTitle
      };
      localStorage.info_ = JSON.stringify(this.info_);
      axios({
        url: "http://localhost:3000/savetodraft",
        method: "post",
        data: data
      }).then(response => {});
    },
    childByValue: function(data) {
      // childValue就是子组件传过来的值
      this.draftTitlefromchild = data;
      console.log("---------------");
      console.log(data);
      console.log("---------------");
    },
    test() {
      axios({
        url: "http://localhost:3000/test",
        method: "get"
      }).then(response => {
        const a = JSON.stringify(response.data.message[8].test);
        this.info_ = JSON.parse(a);
        this.editor.txt.append("this.info_");
      });
    }
  }
};
</script>

<style scoped>
.el-form {
  width: 50%;
}
.el-container {
  height: 100%;
  width: 100%;
}
.editor {
  text-align: center;
  height: 100%;
  margin-bottom: 13rem;
}
.setTag {
  display: flex;
}
.setTag > div {
  line-height: 3rem;
}
</style>